

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>rl_coach.filters.observation.observation_crop_filter &mdash; Reinforcement Learning Coach 0.12.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
    <link href="../../../../_static/css/custom.css" rel="stylesheet" type="text/css">

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> Reinforcement Learning Coach
          

          
            
            <img src="../../../../_static/dark_logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Intro</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../usage.html">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../dist_usage.html">Usage - Distributed Coach</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../features/index.html">Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../selecting_an_algorithm.html">Selecting an Algorithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../dashboard.html">Coach Dashboard</a></li>
</ul>
<p class="caption"><span class="caption-text">Design</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/control_flow.html">Control Flow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/network.html">Network Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/horizontal_scaling.html">Distributed Coach - Horizontal Scale-Out</a></li>
</ul>
<p class="caption"><span class="caption-text">Contributing</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../contributing/add_agent.html">Adding a New Agent</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../contributing/add_env.html">Adding a New Environment</a></li>
</ul>
<p class="caption"><span class="caption-text">Components</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/agents/index.html">Agents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/architectures/index.html">Architectures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/data_stores/index.html">Data Stores</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/environments/index.html">Environments</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/exploration_policies/index.html">Exploration Policies</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/filters/index.html">Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/memories/index.html">Memories</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/memory_backends/index.html">Memory Backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/orchestrators/index.html">Orchestrators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/core_types.html">Core Types</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/spaces.html">Spaces</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/additional_parameters.html">Additional Parameters</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Reinforcement Learning Coach</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>rl_coach.filters.observation.observation_crop_filter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for rl_coach.filters.observation.observation_crop_filter</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Copyright (c) 2017 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">rl_coach.core_types</span> <span class="k">import</span> <span class="n">ObservationType</span>
<span class="kn">from</span> <span class="nn">rl_coach.filters.observation.observation_filter</span> <span class="k">import</span> <span class="n">ObservationFilter</span>
<span class="kn">from</span> <span class="nn">rl_coach.spaces</span> <span class="k">import</span> <span class="n">ObservationSpace</span>


<div class="viewcode-block" id="ObservationCropFilter"><a class="viewcode-back" href="../../../../components/filters/input_filters.html#rl_coach.filters.observation.ObservationCropFilter">[docs]</a><span class="k">class</span> <span class="nc">ObservationCropFilter</span><span class="p">(</span><span class="n">ObservationFilter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Crops the size of the observation to a given crop window. For example, in Atari, the</span>
<span class="sd">    observations are images with a shape of 210x160. Usually, we will want to crop the size of the observation to a</span>
<span class="sd">    square of 160x160 before rescaling them.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">crop_low</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">crop_high</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param crop_low: a vector where each dimension describes the start index for cropping the observation in the</span>
<span class="sd">                         corresponding dimension. a negative value of -1 will be mapped to the max size</span>
<span class="sd">        :param crop_high: a vector where each dimension describes the end index for cropping the observation in the</span>
<span class="sd">                          corresponding dimension. a negative value of -1 will be mapped to the max size</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">crop_low</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">crop_high</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;At least one of crop_low and crop_high should be set to a real value. &quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">crop_low</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">crop_low</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">crop_high</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">crop_high</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">crop_high</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">crop_low</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">crop_low</span> <span class="o">=</span> <span class="n">crop_low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">crop_high</span> <span class="o">=</span> <span class="n">crop_high</span>

        <span class="k">for</span> <span class="n">h</span><span class="p">,</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">crop_high</span><span class="p">,</span> <span class="n">crop_low</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">h</span> <span class="o">&lt;</span> <span class="n">l</span> <span class="ow">and</span> <span class="n">h</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Some of the cropping low values are higher than cropping high values&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">crop_high</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">crop_low</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cropping values cannot be negative&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">crop_low</span><span class="o">.</span><span class="n">shape</span> <span class="o">!=</span> <span class="n">crop_high</span><span class="o">.</span><span class="n">shape</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The low values and high values for cropping must have the same number of dimensions&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">crop_low</span><span class="o">.</span><span class="n">dtype</span> <span class="o">!=</span> <span class="nb">int</span> <span class="ow">or</span> <span class="n">crop_high</span><span class="o">.</span><span class="n">dtype</span> <span class="o">!=</span> <span class="nb">int</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The crop values should be int values, instead they are defined as: </span><span class="si">{}</span><span class="s2"> and </span><span class="si">{}</span><span class="s2">&quot;</span>
                             <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">crop_low</span><span class="o">.</span><span class="n">dtype</span><span class="p">,</span> <span class="n">crop_high</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">crop_size</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">observation_shape</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">Tuple</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]):</span>
        <span class="c1"># replace -1 with the max size</span>
        <span class="n">crop_size</span> <span class="o">=</span> <span class="n">crop_size</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">observation_shape</span><span class="p">)):</span>
            <span class="k">if</span> <span class="n">crop_size</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">crop_size</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">observation_shape</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">crop_size</span>

    <span class="k">def</span> <span class="nf">validate_input_observation_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="p">:</span> <span class="n">ObservationSpace</span><span class="p">):</span>
        <span class="n">crop_high</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_high</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="n">crop_low</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_low</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">crop_high</span> <span class="o">&gt;</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="ow">or</span> \
                <span class="n">np</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">crop_low</span> <span class="o">&gt;</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The cropping values are outside of the observation space&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">is_valid_index</span><span class="p">(</span><span class="n">crop_low</span><span class="p">)</span> <span class="ow">or</span> \
                <span class="ow">not</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">is_valid_index</span><span class="p">(</span><span class="n">crop_high</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The cropping indices are outside of the observation space&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">observation</span><span class="p">:</span> <span class="n">ObservationType</span><span class="p">,</span> <span class="n">update_internal_state</span><span class="p">:</span> <span class="nb">bool</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ObservationType</span><span class="p">:</span>
        <span class="c1"># replace -1 with the max size</span>
        <span class="n">crop_high</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_high</span><span class="p">,</span> <span class="n">observation</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="n">crop_low</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_low</span><span class="p">,</span> <span class="n">observation</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>

        <span class="c1"># crop</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="nb">slice</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">crop_low</span><span class="p">,</span> <span class="n">crop_high</span><span class="p">)]</span>
        <span class="n">observation</span> <span class="o">=</span> <span class="n">observation</span><span class="p">[</span><span class="n">indices</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">observation</span>

    <span class="k">def</span> <span class="nf">get_filtered_observation_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="p">:</span> <span class="n">ObservationSpace</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ObservationSpace</span><span class="p">:</span>
        <span class="c1"># replace -1 with the max size</span>
        <span class="n">crop_high</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_high</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="n">crop_low</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_replace_negative_one_in_crop_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">crop_low</span><span class="p">,</span> <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>

        <span class="n">input_observation_space</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="n">crop_high</span> <span class="o">-</span> <span class="n">crop_low</span>
        <span class="k">return</span> <span class="n">input_observation_space</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018-2019, Intel AI Lab

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>